package com.aqie.easy.base;

/**
 * 504 二进制转化为七进制字符串
 */
public class ConvertToBase7 {
    /**
     * 1, API 2ms
     * @param num
     * @return
     */
    public String convertToBase7(int num) {
        return num >= 0?Integer.toUnsignedString(num, 7):"-"+Integer.toUnsignedString(num*-1,7);
    }

    /**
     * 2, 3ms
     * @param num
     * @return
     */
    public String convertToBase72(int num) {
        if (num == 0) return "0";
        StringBuilder sb = new StringBuilder();
        boolean isMinus = false;//负数的标志位
        if (num < 0) {
            isMinus = true;
            num = -num;
        }
        while (num >= 7) {
            sb.append(num % 7);//加入余数，然后除以7
            num /= 7;
        }
        if (num != 0) {
            sb.append(num);
        }
        //反转后输出即可
        return isMinus ? sb.append("-").reverse().toString() : sb.reverse().toString();
    }

}
